﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class UserControls_jobsearch : System.Web.UI.UserControl
{
    private int totalJobs;
    int searchByCategoryId = -1;
    public int TotalJobs
    {
        get
        {
            return totalJobs;
        }
    }
    public int SearchByCategoryId
    {
        get
        {
            return searchByCategoryId;
        }
        set
        {
            searchByCategoryId = value;
        }
    }

    public DataSet FillSearchByCategoryId(int CatId)
    {
        JobSiteStarterKit.BOL.JobSearch search = new JobSiteStarterKit.BOL.JobSearch();
        return search.SearchBy(JobSiteStarterKit.ENUM.JobSearchBy.CategorySearch, CatId.ToString());
    }
    public void SetSearch(DataSet ds)
    {
        if (ds.Tables.Count > 0) totalJobs = ds.Tables[0].Rows.Count;
        this.grdProject.DataSource = ds;
        this.grdProject.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (searchByCategoryId > -1)
            {
                //use to fill the records
                DataSet ds = FillSearchByCategoryId(searchByCategoryId);
                if (ds.Tables.Count > 0) totalJobs = ds.Tables[0].Rows.Count;
                this.grdProject.DataSource = ds;
                this.grdProject.DataBind();
            }
        }

    }

    public bool EnableSaveButton
    {
        set
        {
            btnSave.Enabled = value;
        }
    }
    public bool EnableApplyButton
    {
        set
        {
            btnApply.Enabled = value;
        }
    }
    protected void grdProject_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }
    protected void grdProject_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void Apply_Click(object sender, EventArgs e)
    {
        if (Context.User.Identity.IsAuthenticated)
        {
            string PostingJobId = "";
            int iCount=0;
            foreach(GridViewRow row in grdProject.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    Control ctrl = row.FindControl("chkJob");
                    if (ctrl != null)
                    {
                        CheckBox chk = (CheckBox)ctrl;
                        if (chk.Checked == true)
                        {
                            PostingJobId += grdProject.DataKeys[row.DataItemIndex].Value.ToString() + "|" ;
                            iCount++;
                        }
                    }
                }
            }
            if (PostingJobId != String.Empty)
            {
                PostingJobId = PostingJobId.Substring(0, PostingJobId.LastIndexOf('|'));
                JobSiteStarterKit.BOL.JobSearch search = new JobSiteStarterKit.BOL.JobSearch();
                int iRegister = search.ApplyJobs(PostingJobId, Context.User.Identity.Name);
                if (iRegister > 0)
                    lblMsg.Text = "<b>You have applied " + Convert.ToString(iCount) + " Jobs.</b>";
                else
                    lblMsg.Text = "<b>You can't apply jobs. Please contact to administrator.</b>";
            }
            else
            {
                lblMsg.Text = "<b>Please select a job.</b>";
            }
        }
        else
            Response.Redirect("login.aspx");
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        if (Context.User.Identity.IsAuthenticated)
        {
            string PostingJobId = "";
            int iCount = 0;
            foreach (GridViewRow row in grdProject.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    Control ctrl = row.FindControl("chkJob");
                    if (ctrl != null)
                    {
                        CheckBox chk = (CheckBox)ctrl;
                        if (chk.Checked == true)
                        {
                            PostingJobId += grdProject.DataKeys[row.DataItemIndex].Value.ToString() + "|";
                            iCount++;
                        }
                    }
                }
            }
            PostingJobId = PostingJobId.Substring(0, PostingJobId.LastIndexOf('|'));
            JobSiteStarterKit.BOL.JobSearch search = new JobSiteStarterKit.BOL.JobSearch();
            search.SaveJobs(PostingJobId, Context.User.Identity.Name);
            lblMsg.Text = "<b>You have saved " + Convert.ToString(iCount) + " Jobs.</b>";
        }
        else
        {
            Response.Redirect("login.aspx");
        }
    }

    
}
